Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ow

Package Overview
Dependencies
Maintainers
2
Versions
44
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ow

Function argument validation for humans

  • 0.18.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
2
Created

What is ow?

The 'ow' npm package is a powerful and expressive validation library for Node.js. It allows developers to validate arguments and inputs in a concise and readable manner. 'ow' provides a variety of built-in validators and supports custom validation logic, making it a versatile tool for ensuring data integrity in applications.

What are ow's main functionalities?

Basic Type Validation

This feature allows you to validate the type of a variable. In this example, the function `validateString` checks if the input is a string. If the input is not a string, an error is thrown.

const ow = require('ow');

const validateString = (input) => {
  ow(input, ow.string);
};

validateString('Hello, World!'); // Passes
validateString(123); // Throws an error

Complex Object Validation

This feature allows you to validate the shape and properties of an object. In this example, the function `validateUser` checks if the user object has a `name` property that is a string with a minimum length of 3 and an `age` property that is a positive integer.

const ow = require('ow');

const validateUser = (user) => {
  ow(user, ow.object.exactShape({
    name: ow.string.minLength(3),
    age: ow.number.integer.positive
  }));
};

validateUser({ name: 'Alice', age: 25 }); // Passes
validateUser({ name: 'Al', age: -5 }); // Throws an error

Custom Validation

This feature allows you to create custom validation logic. In this example, the function `validateEvenNumber` checks if the input is an even number. If the input is not an even number, an error is thrown with a custom message.

const ow = require('ow');

const validateEvenNumber = (input) => {
  ow(input, ow.number.validate(n => ({
    validator: n % 2 === 0,
    message: 'Expected an even number'
  })));
};

validateEvenNumber(4); // Passes
validateEvenNumber(3); // Throws an error

Other packages similar to ow

Keywords

FAQs

Package last updated on 30 Sep 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc